package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.shop.Shop;
import com.motu.monstercity.server.game.commondata.shop.ShopConstant;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.monstercity.server.game.userdata.UserShopLimit;
import com.motu.monstercity.server.game.userdata.UserStage;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;

public class ShopManager extends ShopUserData{
    // 校验shop商品是否解锁,可以购买
    public static boolean checkShopUnlock(UserInfo userInfo, Shop shop) {
        if (shop.getUnlockType() == 0 || shop.getUnlockValue() == 0) {
            return true;
        }
        if (shop.getUnlockType() == ShopConstant.UNLOCK_TYPE_1) {
            return userInfo.getLevel() >= shop.getUnlockValue();
        } else if (shop.getUnlockType() == ShopConstant.UNLOCK_TYPE_2)  {
            UserStage userStage = FightManager.getUserStage(userInfo.getId());
            return userStage.getMapId() >= shop.getUnlockValue();
        } else if (shop.getUnlockType() == ShopConstant.UNLOCK_TYPE_3) {
            return userInfo.getVip() >= shop.getUnlockValue();
        } else if (shop.getUnlockType() == ShopConstant.UNLOCK_TYPE_101002) {
            return true;//默认校验通过,放到下面方法进行校验联盟商店
        } else if (shop.getUnlockType() == ShopConstant.UNLOCK_TYPE_101) {
            long score = UnionScuffleManager.getScuffleScore(userInfo);
            return score >= shop.getUnlockValue();
        }
        return false;
    }

    /**
     * 判断是否超过限购次数
     * @param userShopLimit
     * @param shop
     * @param buyNum 本次的购买数量
     * @return
     */
    public static boolean checkShopLimit(UserShopLimit userShopLimit, Shop shop, int buyNum) {
        if (userShopLimit == null) {
            return true;// 第一次购买
        }
        int num = userShopLimit.takeBuyNum(shop);// 之前的购买次数
        if (num + buyNum <= shop.getLimitNum()) {
            return true;// 可以购买
        } else {
            return false;
        }
    }

    // 更新限购记录数据,并下发给客户端
    public static void updateUserShopLimit(long userId, UserShopLimit userShopLimit, Shop shop, int buyNum, PBUserData.Builder pbUserData) {
        if (shop.getLimitType() == 0) {
            return;// 不做限购
        }
        int newNum = buyNum;// 新的数量
        boolean isInit = false;
        JsonObject jsonObject = new JsonObject();
        if (userShopLimit == null) {
            userShopLimit = new UserShopLimit();
            userShopLimit.putUserId(userId);
            userShopLimit.putShopType(shop.getShopType());
            userShopLimit.putDailyTime(TimeUtils.getCurTime());
            userShopLimit.putWeekTime(TimeUtils.getCurTime());
            userShopLimit.putMonthTime(TimeUtils.getCurTime());
            isInit = true;
        } else {
            newNum = userShopLimit.takeBuyNum(shop) + buyNum;
            jsonObject = new JsonObject(userShopLimit.takeBuyStr(shop));
        }
        String key = shop.getId() + "";
        jsonObject.put(key, newNum);
        if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_1) {//  每日限购
            userShopLimit.putDailyBuy(jsonObject.toString());
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_2) {// 每周限购
            userShopLimit.putWeekBuy(jsonObject.toString());
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_3) {// 每月限购
            userShopLimit.putMonthBuy(jsonObject.toString());
        } else if (shop.getLimitType() == ShopConstant.SHOP_LIMIT_TYPE_4) {// 终身限购
            userShopLimit.putTotalBuy(jsonObject.toString());
        }
        if (isInit) {// 第一次创建userShopLimit 数据
            userShopLimit.doCreate();
            ProtoDataUtils.updatePBUserData(pbUserData, userShopLimit, true);
        } else {
            userShopLimit.update();
            ProtoDataUtils.updatePBUserData(pbUserData, userShopLimit);
        }
    }
}
